from JoyStick_Shield import Shield
from machine import SPI, Pin,ADC
import st7789
import urandom
import utime
spi = SPI(0, baudrate=40000000, polarity=1, phase=0, bits=8, endia=0, sck=Pin(6), mosi=Pin(8))
display = st7789.ST7789(spi, 240, 240, reset=Pin(11,func=Pin.GPIO, dir=Pin.OUT), dc=Pin(7,func=Pin.GPIO, dir=Pin.OUT))
#display.init()
backgroud_clr = st7789.color565(0, 0, 0)
cell_clr = st7789.color565(255, 0, 0)
wall_clr = st7789.color565(255,255,255)
snake_clr = st7789.color565(0,255,0)
food_clr = st7789.color565(0,123,123)

start_x = 4
start_y = 4
cell_len = 8
step = 12

user_rows = 22
user_columns = 22

def Op():
    display.init()
    display.draw_string(50, 230, "Powered by BlackWalnuut Labs.")
    utime.sleep(2)
    return

def Lis():
    display.fill(st7789.color565(0, 0, 0))
#    display.pixel(5, 5, st7789.color565(255, 0, 0))
    display.draw_string(10, 10, "Hungry Snake",size=3,color=st7789.color565(255, 255, 255))
    display.draw_string(10, 40, "APP 2",size=3,color=st7789.color565(255, 255, 255))
    return

def draw_board():
        display.fill(st7789.color565(0, 0, 0))
        x=start_x
        y=start_y
        step = cell_len + 4

        for i in range(user_rows):
            for j in range(user_columns):
                display.fill_rect(x,y,cell_len,cell_len, cell_clr)
                x += step
            x = start_x
            y += step
        return
def draw_wall():
    display.fill(st7789.color565(0,0,0))
    x=start_x
    y=start_y
    step=cell_len + 4

    for i in range(user_rows):
            for j in range(user_columns):
                if i == 0 or j == 0 or i == 19 or j == 19:
                    display.fill_rect(x,y,cell_len,cell_len, wall_clr)
                x += step
            x = start_x
            y += step
    return


shield=Shield()
class Arrow:
    def __init__(self, num):
        self.num = num
        return

    def ArrAction(self):
        Lis()
        display.draw_string(200, self.num*30-15, "<-", size=2, color=st7789.color565(255, 255, 255))
        return

    def MoveOn(self):
        self.num = self.num-1
        self.ArrAction()
        return

    def MoveDown(self):
        self.num = self.num+1
        self.ArrAction()
        return
Arr=Arrow(1)

def snaker():
    square_position = 1
    square_purpose = [10,10]
    snakeposition = [8,8]
    snakelength = [[8,8],[7,8],[6,8]]
    head = 3
    tail = 0
    derection = 'right'
    change_derection = derection
    draw_wall()    
    while True:
        Flag = shield.read()
        if not Flag == 0:
            utime.sleep(0.3)
        if Flag == 1:
            change_derection = 'right'
        if Flag == 2:
            change_derection = 'left'
        if Flag == 3:
            change_derection = 'up'
        if Flag == 4:
            change_derection = 'down'    
        if change_derection == 'left' and not derection == 'right':
            derection = change_derection
        if change_derection == 'right' and not derection == 'left':
            derection = change_derection
        if change_derection == 'up' and not derection == 'down':
            derection = change_derection
        if change_derection == 'down' and not derection == 'up':
            derection = change_derection
            # 根据方向，改变坐标
        if derection == 'left':
            snakeposition[0] -= 1
        if derection == 'right':
            snakeposition[0] += 1
        if derection == 'up':
            snakeposition[1] -= 1
        if derection == 'down':
            snakeposition[1] += 1
        snakelength.insert(0,list(snakeposition))
        if snakeposition[0] == square_purpose[0] and snakeposition[1] == square_purpose[1]:
            square_position = 0
        else:
            snakelength.pop()
        if square_position == 0:
            square_purpose[0] = urandom.randint(1,18)
            square_purpose[1] = urandom.randint(1,18)
            square_position = 1
        # draw_wall()
        display.fill_rect(step + start_x,step + start_y,18 * step,18 * step,backgroud_clr)
        for position in snakelength:
            display.fill_rect(position[0]*step+start_x,position[1]*step+start_y,cell_len,cell_len,snake_clr)
        display.fill_rect(square_purpose[0]*step+start_x,square_purpose[1]*step+start_y,cell_len,cell_len,food_clr)
        if snakeposition[0] <= 0 or snakeposition[0] >= 19:
            break
        if snakeposition[1] <= 0 or snakeposition[1] >= 19:
            break
        isdead=0
        for snakebody in snakelength[1:]:
            if snakeposition[0] == snakebody[0] and snakeposition[1] == snakebody[1]:
                isdead=1
                break
        if isdead==1:
            break

Op()
Arr.ArrAction()

square_purpose = [10,10]
while True:
    Flag=shield.read()
    print(Flag)
    if Flag==3 and Arr.num>1:
        Arr.MoveOn()
    if Flag==4 and Arr.num<8:
        Arr.MoveDown()
    if Flag==5 and Arr.n:
        
        display.fill(backgroud_clr)
        display.draw_string(10, 100, "Game Begin",size=4,color=st7789.color565(255, 255, 255))
        #display.draw_string(90,150,"Please Press A",size=1,color=st7789.color565(255,255,255))
        utime.sleep(2)
        while True:
            Flag=shield.read()
            if Flag==8:
                break
            draw_board()
            utime.sleep(2)
            draw_wall()
            snaker()
            display.fill(backgroud_clr)
            display.draw_string(10,100,"Game Over",size = 4,color=st7789.color565(255,255,255))
            utime.sleep(2)
            Arr.ArrAction()
            break
            
        if Flag==8:
            Arr.ArrAction()
            continue